﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Microsoft.VisualBasic.PowerPacks;

namespace BattleshipGameClient
{

    [Serializable]
    public class Ship
    {
        public enum Type { Carrier, Battleship, Destroyer, PatrolBoat, Submarine, None };
        public enum Position { Vertical, Horizontal };
        private Type type;
        private int size;
        private List<Point> hits = new List<Point>();
        private List<Board.Cell> graphShip;
        public List<Board.Cell> GraphShip
        {
            get { return graphShip; }
            set { graphShip = value; }
        }

        private int hitCount;
        private bool isSunk = false;

        public bool IsSunk
        {
            get { return isSunk; }
        }

        public Ship(Type type)
        {
            this.type = type;
            

            switch (this.type)
            {
                case Type.Carrier:
                    this.size = 5;
                    break;
                case Type.Battleship:
                    this.size = 4;
                    break;
                case Type.Destroyer:
                    this.size = 3;
                    break;
                case Type.PatrolBoat:
                    this.size = 3;
                    break;
                case Type.Submarine:
                    this.size = 2;
                    break;
            }
        }

        public Ship(Type type, List<Board.Cell> graphShip) : this (type)
        {
            this.graphShip = graphShip;
        }


        public bool shoot(Point coordinate) {
            // Check if coordinate is within the ship
            foreach(Board.Cell shipCell in graphShip)
            {
                Point coordCell = new Point( Convert.ToInt32(shipCell.GraphCell.Name.Split(';')[0]), Convert.ToInt32(shipCell.GraphCell.Name.Split(';')[1]) );
                if(coordCell.Equals(coordinate))
                {
                    if (!hits.Contains(coordinate)) // The coordinate was not hit before
                    {
                        hits.Add(coordinate);
                        addHit();
                    }
                    return true;
                }
            }
            return false;
        }

        public Type getShipType()
        {
            return type;
        }

        internal void addHit()
        {
            hitCount++;
            if (hitCount == size)
            {
                isSunk = true;
            }
        }
    }
}
